package com.xkdx.caipiao.module.network;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.entity.mime.content.FileBody;

import android.graphics.Bitmap;

import com.xkdx.caipiao.statics.IConstants;

public abstract class AbsAction {
	public String url = IConstants.addressV2;
	public Object requestData;

	public Bitmap bitmap;
	public FileBody fileBody;
	public FileBody editFileBody, editeCutFileBody;
	public int key;


	public abstract void constructRequest();
	
	public String constructMod(List<Parameter> list) {
		StringBuffer json = new StringBuffer();
		if (list.size() == 1) {
			String s = "[{\"Mod\":\"" + list.get(0).Mod + "\",\"Act\": \""
					+ list.get(0).getAction() + "\",\"Content\": "
					+ list.get(0).getJson() + "}]";
			json.append(s);
		} else {
			for (int i = 0; i < list.size(); i++) {
				if (i == 0) {
					String s = "[{\"Mod\":\"" + list.get(i).Mod
							+ "\",\"Act\": \"" + list.get(i).getAction()
							+ "\",\"Content\": " + list.get(i).getJson() + "},";
					json.append(s);
				} else if (i > 0 && i < list.size() - 1) {
					String s = "{\"Mod\":\"" + list.get(i).Mod
							+ "\",\"Act\": \"" + list.get(i).getAction()
							+ "\",\"Content\": " + list.get(i).getJson() + "},";
					json.append(s);
				} else if (i == list.size() - 1) {
					String s = "{\"Mod\":\"" + list.get(i).Mod
							+ "\",\"Act\": \"" + list.get(i).getAction()
							+ "\",\"Content\": " + list.get(i).getJson() + "}]";
					json.append(s);
				}
			}

		}
		return json.toString();

	}

	public String constructJson(HashMap<String, String> map) {
		StringBuffer sb = new StringBuffer();
		int i = 0;
		Iterator iterator = map.entrySet().iterator();
		if (!iterator.hasNext()) {
			sb.append("{}");
		}
		
		while (iterator.hasNext()) {
			i++;
			Map.Entry s = (Entry) iterator.next();

			if (map.size() == 1) {;				sb.append("{\"" + s.getKey() + "\":\"" + s.getValue() + "\"}");
			} else {
				if (i == 1) {
					sb.append("{\"" + s.getKey() + "\":\"" + s.getValue()
							+ "\",");
				} else if (i > 1 && i < map.size()) {
					sb.append("\"" + s.getKey() + "\":\"" + s.getValue()
							+ "\",");
				} else if (i == map.size()) {
					sb.append("\"" + s.getKey() + "\":\"" + s.getValue()
							+ "\"}");
				}
			}
		}

		return sb.toString();
	}

	public class Parameter {
		public String Mod;
		String action;
		String json;

		public Parameter(String Mod, String action, String json) {
			this.Mod = Mod;
			this.action = action;
			this.json = json;
		}

		public String getMod() {
			return Mod;
		}

		public void setMod(String mod) {
			Mod = mod;
		}

		public String getAction() {
			return action;
		}

		public void setAction(String action) {
			this.action = action;
		}

		public String getJson() {
			return json;
		}

		public void setJson(String json) {
			this.json = json;
		}

	}

}
